package cn.ekuma.odbo.mobile.category.ui

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.recyclerview.widget.RecyclerView
import cn.ekuma.odbo.mobile.R
import com.gitee.wsl.struct.category.CategoryItemWarp


abstract class TreeViewBinder<VH : RecyclerView.ViewHolder>(val layoutId: Int) {

    abstract fun provideViewHolder(itemView: View): VH

    abstract  fun bindView(holder: VH, position: Int, node: CategoryItemWarp)

    open class ViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView) {
        fun <T : View> findViewById(@IdRes id: Int): T {
            return itemView.findViewById<View>(id) as T
        }
    }
}

class DirectoryNodeBinder:TreeViewBinder<DirectoryNodeBinder.ViewHolder>(R.layout.item_dir){

    override fun provideViewHolder(itemView: View): ViewHolder {
        return  ViewHolder(itemView)
    }

    override fun bindView(
        holder: ViewHolder,
        position: Int,
        node: CategoryItemWarp
    ) {
        holder.ivArrow.setRotation(0f)
        holder.ivArrow.setImageResource(android.R.drawable.arrow_down_float)
        val rotateDegree = if (node.isExpand) 90 else 0
        holder.ivArrow.rotation = rotateDegree.toFloat()
        //val dirNode= node.data()
        holder.tvName.text= node.title()
        if (node.isLeaf)
            holder.ivArrow.visibility = View.INVISIBLE
        else
            holder.ivArrow.visibility = View.VISIBLE
    }


    class ViewHolder(rootView: View):RecyclerView.ViewHolder(rootView){
        val ivArrow=rootView.findViewById<ImageView>(R.id.iv_arrow)
        val tvName=rootView.findViewById<TextView>(R.id.tv_name)
    }
}

class FileNodeBinder:TreeViewBinder<FileNodeBinder.ViewHolder>(R.layout.item_file){

    override fun provideViewHolder(itemView: View): ViewHolder {
        return ViewHolder(itemView)
    }

    override fun  bindView(
        holder: ViewHolder,
        position: Int,
        node: CategoryItemWarp
    ) {
        holder.tvName.text=node.title()
    }


    class ViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView) {
        val tvName=rootView.findViewById<TextView>(R.id.tv_name)
    }

}
